﻿package idv.cjcat.monica.platformer.player {
	import idv.cjcat.rusher.engine.logger.LogType;
	import idv.cjcat.rusher.geom.Vec2D;
	
	internal class WallingState extends PlayerState {
		
		public function WallingState(controller:PlayerController, direction:Number) {
			super(controller, direction);
			controller.engine.logger.log("Entered state: WallingState", LogType.INFO, controller);
			
			var vel:Vec2D = getVelocity();
			vel.y = 0;
			setVelocity(vel);
			
			this.controller.jumpCount = 0;
		}
		
		override public function startJump():void {
			if (controller.jumpCount < PlayerController.MAX_JUMPS) {
				controller.state = new ReboundJumpingUpState(controller, -controller.wallingDirection);
				setPlayerMovieClip(renderTarget.wallRebound, false);
				renderTarget.direction = -controller.wallingDirection;
			}
		}
		
		override public function onTick(time:Number):void {
			if (controller.onWall) {
				if (controller.onFloor) {
					if (controller.moving && (controller.wallingDirection == controller.wallingDirection)) {
						setState(WallPushingState);
						setPlayerMovieClip(renderTarget.landing, false);
					} else {
						setState(StandingState);
						setPlayerMovieClip(renderTarget.landing, false);
					}
					return;
				}
				
				//var vel:Vec2D = getVelocity();
				//vel.x = controller.wallingDirection * PlayerController.WALKING_SPEED;
				//setVelocity(vel);
				//return;
				
				if (controller.moving) {
					if (controller.direction == -controller.wallingDirection) {
						setState(FallingDownState);
						setPlayerMovieClip(renderTarget.fallingDown, false);
						renderTarget.direction = -controller.wallingDirection;
						return;
					} else {
						//var vel:Vec2D = getVelocity();
						//vel.x = direction * PlayerController.WALKING_SPEED;
						//setVelocity(vel);
					}
				}
				
			} else {
				var vel:Vec2D = getVelocity();
				vel.y *= PlayerController.JUMP_CANCEL_FACTOR;
				setVelocity(vel);
				setState(FallingDownState);
				setPlayerMovieClip(renderTarget.fallingDown, false);
				return;
			}
		}
	}
}